import { AutoGuideStepBase } from "./AutoGuideStepBase";
import { IAutoGuideSteper } from "./IAutoGuideSteper";
import { AutoGuideData } from "../AutoGuideData";
import { Window } from "../../../../../component/window/Window";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { Cache } from "../../../../cache/Cache";
import { ItemData } from "../../../../resource/info/ItemData";
import { GameController } from "../../../../mvc/GameController";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../../events/DataEvent";
import { EventName } from "../../../../mvc/EventName";
import { _itemData } from "../../../../../../this/_itemData";
type int = number;
//class AutoGuideBackPackSteper
    
    export  class AutoGuideBackPackSteper extends AutoGuideStepBase implements IAutoGuideSteper
    {
        constructor()
        {
            super();
            return;
        }

        public guide(arg1: AutoGuideData, arg2: Window=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=null;
            _data = arg1;
            if (String(arg1.guideTypeValue).indexOf("code:") >= 0) 
            {
                loc2 = int(String(arg1.guideTypeValue).substr(5));
                if ((loc3 = ItemConfig.instance.getInfoByCode(loc2).item.category) != ECategory._ECategoryAmulet) 
                {
                    loc1 = Cache.instance.pack.backPackCache.getItemByCode(loc2);
                }
                else 
                {
                    loc1 = Cache.instance.pack.magicWeaponPackCache.getItemByCode(loc2);
                }
            }
            if (String(arg1.guideTypeValue).indexOf("category:") >= 0) 
            {
                this._searchCategoryValue = int(String(arg1.guideTypeValue).substr(9));
                if (this._searchCategoryValue != ECategory._ECategoryAmulet) 
                {
                    loc4 = Cache.instance.pack.backPackCache.getItemsByJudge(this.isMyCategory);
                }
                else 
                {
                    loc4 = Cache.instance.pack.magicWeaponPackCache.getItemsByJudge(this.isMyCategory);
                }
                if (loc4 == null || loc4.length == 0 || loc4[0] == null) 
                {
                    return;
                }
                loc1 = loc4[0] as ItemData;
            }
            if (loc1 == null) 
            {
                return;
            }
            this._itemData = loc1;
            this.stop();
            if (!GameController.pack.isViewShow) 
            {
                GameController.pack.view.show();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_UseAnyItemByConfig, {"itemData": _itemData, "type": arg1.guideType, "typeParams": arg1.backPackUseItemTips}));
            Dispatcher.addEventListener(EventName.AutoGuideItemUsed, this.guideEndHandler);
            return;
        }

        /* internal  */isMyCategory(arg1: ItemData): Boolean
        {
            if (arg1.category == this._searchCategoryValue) 
            {
                return true;
            }
            return false;
        }

        /* internal  */guideEndHandler(arg1: DataEvent): void
        {
            this.stop();
            Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, _data));
            return;
        }

        public stop(arg1: AutoGuideData=null): void
        {
            Dispatcher.removeEventListener(EventName.AutoGuideItemUsed, this.guideEndHandler);
            return;
        }

        /* internal */ /* var */_itemData: ItemData;

        /* internal */ /* var */_searchCategoryValue: int=-1;
    }
